<template>
  <div class="salesmanList">
    <el-card class="taskList">
      <el-form class="s_form" :inline="true">
        <div class="left">
          <el-form-item>
            <div class="cardName">人员列表</div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="salesmanAdd">新增人员</el-button>
          </el-form-item>
        </div>
        <div class="right">
          <el-form-item label="所属部门">
            <el-select v-model="dataForm.roleId" @change="getDataList" clearable>
              <el-option v-for="(item, index) in roleList" :key="index" :label="item.roleName" :value="item.roleId"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList.filter(item=>item.userId!=='1')" border :height="tabHeight">
        <el-table-column v-for="row in headerList" :key="row.key" :prop="row.key" :min-width="row.width"
          :label="row.label" show-overflow-tooltip header-align="center" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="salesmanEdit(scope.row)">编辑人员</el-button>
            <el-button type="text" size="small" @click="salesmanDelete(scope.row)">删除人员</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page.sync="curPage" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </el-card>
    <salesman-dialog ref="salesmanDialog" @refresh="getDataList"/>
  </div>
</template>

<script>
import salesmanDialog from './salesmanComponent/salesmanDialog.vue'
export default {
  components: { salesmanDialog },
  data () {
    return {
      dataListLoading: false,
      dataForm: {
        roleId: null
      },
      dataList: [],
      // 资料列表字段
      headerList: [{
        key: 'userId',
        label: '人员编号',
        width: '150'
      },
      {
        key: 'username',
        label: '人员名称',
        width: '150'
      },
      {
        key: 'mobile',
        label: '人员电话',
        width: '100'
      },
      {
        key: 'email',
        label: '人员邮箱',
        width: '100'
      },
      {
        key: 'roleName',
        label: '所属部门',
        width: '100'
      }],
      roleList: [],
      tabHeight: '600',
      // 分页
      curPage: 1,
      total: 0,
      limit: 10
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tabHeight = this.$getClientHeight() - 300 + 'px'
    })
  },
  activated () {
    this.getDataList()
    this.getroleList()
  },
  methods: {
    // 新增人员
    salesmanAdd () {
      this.$refs.salesmanDialog.init()
    },
    // 编辑人员
    salesmanEdit (row) {
      this.$refs.salesmanDialog.init(row)
    },
    // 删除人员
    salesmanDelete (row) {
      this.$confirm('您确认要删除 ' + row.username + '吗？', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/sys/user/delete', [row.userId]).then(({ data: res }) => {
          let { code, msg } = res
          if (code === 0) {
            this.$message.success('操作成功!')
            this.getDataList()
          }
          if (code !== 0) {
            this.$message.error(msg)
          }
        })
      }).catch(() => {
      })
    },
    // 获取数据
    getDataList () {
      this.dataListLoading = true
      this.$http.get('/sys/user/list', {
        params: {
          limit: this.limit,
          page: this.curPage,
          ...this.dataForm
        }
      }).then(({ data: res }) => {
        let { code, msg, page } = res
        console.log(code, msg, page)
        this.dataList = []
        this.total = 0
        if (code === 0 && page) {
          let { list, total } = page
          this.dataList = list && list.length ? list : []
          this.total = total
          this.dataListLoading = false
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
        this.dataListLoading = false
      })
    },
    // 获取部门数据
    getroleList () {
      this.$http.get('/sys/role/select').then(({ data: res }) => {
        let { code, msg, list } = res
        console.log(code, msg, list)
        this.roleList = []
        if (code === 0 && list) {
          this.roleList = list && list.length ? list : []
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
      })
    },
    // 切换每页数量
    pageSizeChangeHandle (size) {
      console.log(size)
      this.limit = size
      this.curPage = 1
      this.getDataList()
    },
    // 切换页
    pageCurrentChangeHandle (curPage) {
      this.curPage = curPage
      this.getDataList()
    }
  }
}
</script>

<style lang="scss">
.salesmanList {
  padding: 20px;
  .taskList {
    width: 100%;
    min-width: 1100px;
    background-color: rgba(255, 255, 255, 1);
    margin-top: 15px;
    .s_form {
      display: flex;
      justify-content: space-between;
      .cardName {
        font-weight: bold;
        font-size: 18px;
      }
      .s_input {
        width: 100px;
      }
    }
    .sonList {
      background-color: rgba(242, 242, 242, 0.5);
      padding: 10px;
      .son {
        display: flex;
        align-items: center;
        justify-content: space-around;
        flex-wrap: wrap;
        justify-content: start;
        div {
          width: 30%;
          margin: 10px;
        }
      }
    }
  }
  .status_1::before,
  .status_2::before {
    content: "";
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 5px;
    background-color: #f04134;
  }
  .status_2::before {
    background-color: #00a854;
  }
  .cancel {
    color: #f56c6c;
  }
}
</style>
